package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.data.YueFanAdapter;
import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SearchYueFanTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/21.
 */
public class SearchYueFanListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private YueFanAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int searchTaskId = -1;
    private String keywords;
    private TextView back;
    private NoDataUtil mNoData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_yuefan_list);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            keywords = intent.getStringExtra("keyword");
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshListView)findViewById(R.id.search_yuefan_pull_list);
        mListView.init(mContext,true);
        mAdapter = new YueFanAdapter(mContext);
        mListView.setAdapter(mAdapter,true);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListView.getHeaderViewsCount() > 0) {
                    position--;
                }
                if (position < 0) return;
                HuoDongInfo fdinfo = (HuoDongInfo) mAdapter.getItem(position);
                String activeid = fdinfo.getActivityId();
                String icon = fdinfo.getUserLogo();
                Intent bdInent = new Intent(mContext, YuFanDetailActivity.class);
                bdInent.putExtra("ACTIVEID", activeid);
                bdInent.putExtra("LOGO", icon);
                startActivity(bdInent);
            }
        });
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
        RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext,contentLayout);

    }


    private void getPaiHaiBang() {
        if(searchTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        SearchYueFanTask task = new SearchYueFanTask(keywords,size, currentPage);
        task.addListener(anchorListListener);
        searchTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == searchTaskId){
                searchTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<HuoDongInfo> fansList = new ArrayList<HuoDongInfo>();
                if(data != null){
                    fansList = (List<HuoDongInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                currentPage = result;
                if (mAdapter != null && mAdapter.getCount()== 0){
                    mNoData.ListViewEmpty(mListView,null,null,-1);
                }else {
                    mNoData.ListViewNoEmpty(mListView);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == searchTaskId){
                searchTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };


    public void onHiddenChanged(boolean hidden) {
        onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
                mListView.doRefresh();
            }

        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getPaiHaiBang();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getPaiHaiBang();
    }
}
